package org.neowei.image.filtering.handle;

import java.awt.image.BufferedImage;

import org.neowei.image.api.ImageOption;
import org.neowei.image.filter.Filter;
import org.neowei.image.filter.FilterOption;
import org.neowei.image.filtering.HorizontalFiltering;
import org.neowei.image.filtering.VerticalFiltering;

public class ResizeHandle implements IHandle {

	@Override
	public BufferedImage getBufferedImage(ImageOption imageOption, BufferedImage srcimage) {
		imageOption.setBGFinalWH();
		BufferedImage outimage;
		if (imageOption.getFinalwidth() == 0 || imageOption.getFinalheight() == 0) {
			outimage = srcimage;
		} else {
			FilterOption contributor = Filter.getContrib(imageOption.getFinalwidth(), imageOption.getSrcwidth());
			BufferedImage horbuffer = new HorizontalFiltering(contributor, imageOption.getSrcwidth(), imageOption.getSrcheight(), imageOption.getFinalwidth(), srcimage).getBufferedImage();
			outimage = new VerticalFiltering(contributor, imageOption.getSrcheight(), imageOption.getFinalwidth(), imageOption.getFinalheight(), horbuffer).getBufferedImage();
		}
		return outimage;
	}
}
